import Inventories from '../business/inventories';
import Inventoryitem from '../business/inventoryitem';
import Modifiers from './modifiers';
import lodash from 'lodash';
export default class Cart {
  constructor() {
    // this.qty = 0;
    this.items = [];
  }

  //get&&get
  get qty() {
    return this.items.length;
  }
  set qty(value) {
    return value;
  }


  _pushInventoryitem(inventoryitem) {
    this.items.push(inventoryitem);
  }
  _deleteInventoryitem(inventoryitem, index = 'undefined') {
    // debugger
    let i = index;
    if (i == 'undefined') {
      i = this.items.indexOf(inventoryitem);
    }
    if (i >= 0) {
      this.items.splice(i, 1);
    }
  }
  getCartByInventoryidAndmodifiers(inventory, modifiers) {
    let res = null;
    let array = this.getCartByInvrntortyId(inventory.id);
    if (array && array.length) {
      for (var i = 0, l = array.length; i < l; i++) {
        if (lodash.isEqual(array[i].modifiers, modifiers)) {
          res = array[i]
        }
      }
    }
    return res;
  }

  //增加有选项的商品
  addInventoryOfHasRequired(inventory, modifiers) {
    // debugger
    let inventoryitem = this.getCartByInventoryidAndmodifiers(inventory,
      modifiers);
    if (!inventoryitem) {
      inventoryitem = new Inventoryitem(inventory);
      this._pushInventoryitem(inventoryitem);
      inventoryitem.modifiers = modifiers;
      inventoryitem.price = inventory.price;
      inventoryitem.qty++;
    } else {
      inventoryitem.qty++;
    }
    return inventoryitem;
  }

  addInventory(inventory) {
      let res = true;
      if (!Inventories.isInventoryIsRequired(inventory)) {
        const inventoryitem = Cart.isCartExistInventoryIdAndNoOption(inventory.id,
          this.items);
        if (inventoryitem) {
          //存在这个商品
          // let inventoryitem = this._findInventoryitemByInventoryId(inventory.id);
          inventoryitem.qty++;
          inventoryitem.price = inventory.price;
        } else {
          //加入一个Inventoryitem
          let inventoryitem = new Inventoryitem(inventory);
          this.qty++;
          this._pushInventoryitem(inventoryitem);
          inventoryitem.qty++;
          inventoryitem.price = inventory.price;
          inventoryitem.inventoryCode = inventory.code;

        }
      } else {
        //弹出选项界面
        res = false;
      }
      return res;
    }
    //删除商品
  subtrackInventory(inventory) {
      let res = true;
      const _self = this;
      //存在这个商品
      // return item !==undefined && item!==null
      let inventoryItem = _self._findInventoryitemByInventoryId(inventory.id);
      // debugger
      if (inventoryItem && inventoryItem.qty == 1) {
        //删除这个item
        // console.log(inventoryItem.inventoryName);
        this._deleteInventoryitem(inventoryItem);
      } else if (inventoryItem && inventoryItem.qty) {
        inventoryItem.qty--;
      }
      return res;
    }
    //获取同意商品id的项
  getCartDeepByInvrntortyId(inventoryId) {
      let res = [];
      this.items.forEach((elem) => {
        if (elem.inventoryId == inventoryId) {
          //深拷贝
          // var newObject = Object.create(oldObject);
          let o = lodash.clone(elem);
          res.push(o);
          // res.push(elem);
        }
      });
      // res = this.items.filter(function(item) {
      //   return item.inventoryId == inventoryId;
      // });
      return res;
    }
    //获取同意商品id的项
  getCartByInvrntortyId(inventoryId) {
      let res = [];
      this.items.forEach((elem) => {
        if (elem.inventoryId == inventoryId) {
          res.push(elem);
        }
      });
      // res = this.items.filter(function(item) {
      //   return item.inventoryId == inventoryId;
      // });
      return res;
    }
    //商品详情的选项和购物车的选项合并 这里可以优化 虚拟dom的算法 todo
  mergeInventoryCartByInventoryId(cart, inventoryID) {
      //先删除原来与inventoryid一样的选项，再添加
      // debugger
      const self = this;
      const inventoryId = (cart.items[0] && cart.items[0].inventoryId) ||
        inventoryID;
      // debugger
      let array = self.items;
      // debugger
      for (let i = 0, l = array.length - 1; i <= l; i++) {
        if (array[i].inventoryId === inventoryId) {
          array.splice(i, 1);
          i--;
          l--;
        }
      }
      // debug
      //添加商品
      // self.items.concat(cart.items[0]);
      cart.items.forEach((elem) => {
        self.items.push(elem);
      });
      self.qty = self.items.length;
    }
    //清空购物车
  emptyCart() {
      // debugger
      this.qty = 0;
      this.items.splice(0, this.items.length);
    }
    //购物车里是否已经存在这个商品id
  static isCartExistInventoryId(inventoryId, inventoryitems) {
    let res = false;
    var arr = inventoryitems;
    for (let i = 0, l = arr.length; i < l; i++) {
      if (arr[i].inventoryId === inventoryId) {
        res = true;
        break;
      }
    }
    return res;
  }

  static isCartExistInventoryIdAndNoOption(inventoryId, inventoryitems) {
      let res = false;
      var arr = inventoryitems;
      for (let i = 0, l = arr.length; i < l; i++) {
        if (arr[i].inventoryId === inventoryId) {
          if (!(arr[i].modifiers.items && arr[i].modifiers.items[0] && arr[i].modifiers
              .items[0].options[
                0])) {
            res = arr[i];
            break;
          }
        }
      }
      return res;
    }
    //商品加1
  increaseInventoryInInventoryitem(index) {
      // console.log(cartItems);
      this.items[index].qty++;
    }
    //商品减1
  decreaseInventoryInInventoryitem(index) {
    // console.log(cartItems);
    this.items[index].qty--;
    if (this.items[index].qty == 0) {
      this.items.splice(index, 1);
    };
  }

  setNewCart(cart) {
    this.items = [];
    cart.items.forEach(v => {
      let item = new Inventoryitem();
      item.setNewInventoryitem(v);
      this.items.push(item);
    })
  }

  //通过id返回商品,
  _findInventoryitemByInventoryId(inventoryId) {
    let res;
    var arr = this.items;
    for (let i = 0, l = arr.length; i < l; i++) {
      if (arr[i].inventoryId === inventoryId) {
        res = arr[i];
        break;
      }
    }
    return res;
  }

  toJSON() {
    return lodash.extend({}, lodash.clone(this), {
      qty: this.qty
    });
  }
}
